﻿package net.sealuitk.containers 
{
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import net.sealuitk.Container;
	import net.sealuitk.UIConst;
	/**
	 * ...
	 * @author mkn
	 */
	public class Column extends Container {
		
		public function Column(Name:String, Width:Number, Height:Number, AlignHorizontal:String = UIConst.ALIGN_CENTER, AlignVertical:String = UIConst.ALIGN_CENTER) {
			setAlignments(AlignHorizontal, AlignVertical);
			super(Name, Width, Height);
		}
		
		override public function updateFixed():void {
			var flowX:Number = 0;
			var flowY:Number = 0;
			var maxWidth:Number = 0;
			var maxHeight:Number = 0;
			var gadgetWidth:Number = 0;
			var gadgetHeight:Number = 0;
			
			var i:int = 0
			
			for (i = 0; i < contentFixed.length ; i++) {
				maxWidth = Math.max(contentFixed[i].width, maxWidth);
				maxHeight = Math.max(contentFixed[i].height, maxHeight);
				gadgetWidth += contentFixed[i].width;
				gadgetHeight += contentFixed[i].height;
			}
			
			if (sizeSet == false) {
				size.x = padding.left + padding.right + maxWidth;
				size.y = padding.top + padding.bottom + gadgetHeight + (contentFixed.length + 1) * contentPadding.y;
			}
			
			switch(alignVertical) {
				case UIConst.ALIGN_TOP:
					flowY = padding.top;
					flowY += contentPadding.y;
					for (i = 0; i < contentFixed.length ; i++) {
						contentFixed[i].y = flowY;
						flowY += contentFixed[i].height + contentPadding.y;
					}
				break;
				case UIConst.ALIGN_CENTER:
					flowY = ( height - (padding.top + padding.bottom) ) / 2 - ( gadgetHeight + ( (contentFixed.length + 1 ) * contentPadding.y) ) / 2;
					flowY += padding.top;
					flowY += contentPadding.y;
					
					for (i = 0; i < contentFixed.length ; i++) {
						contentFixed[i].y = flowY;
						flowY += contentFixed[i].height + contentPadding.y;
					}
				break; 
				case UIConst.ALIGN_BOTTOM:
					flowY = height - padding.bottom;
					for (i = contentFixed.length ; --i > -1 ; ) {
						flowY -= ( contentFixed[i].height + contentPadding.y );
						contentFixed[i].y = flowY;
					}
				break;
				case UIConst.ALIGN_EVEN:
					var between:Number = (height - gadgetHeight - (padding.top + padding.bottom)) / (contentFixed.length + 1);
					flowY = padding.top + between;
					for (i = 0; i < contentFixed.length ; i++) {
						contentFixed[i].y = flowY;
						flowY += contentFixed[i].height + between;
					}
				break;
				case UIConst.ALIGN_MAGIC:
					// TODO: alignHorizontal MAGIC
					throw Error("Not implemented");
				break;
				default:
					throw Error("Inappriate vertical alignment: " + alignVertical );
			}
			
			switch(alignHorizontal) {
				case UIConst.ALIGN_EVEN:
				case UIConst.ALIGN_MAGIC:
				case UIConst.ALIGN_LEFT:
					for (i = 0; i < contentFixed.length ; i++) {
						contentFixed[i].x = padding.left + contentPadding.x;
					}
				break;
				case UIConst.ALIGN_MIDDLE:
					for (i = 0; i < contentFixed.length ; i++) {
						contentFixed[i].x = (width - contentFixed[i].width) / 2;
						contentFixed[i].x += padding.left + contentPadding.x;
					}
				break;
				case UIConst.ALIGN_RIGHT:
					for (i = 0; i < contentFixed.length ; i++) {
						contentFixed[i].x = width - contentFixed[i].width - padding.right - contentPadding.x;
					}
				break;
				default:
					{
						throw Error("Inappropriate horizontal alignment: " + alignHorizontal );
					}
			}
			
			super.updateFixed();
			
			if (sizeSet == false) {
				updateBackground();
				updateFloat();
				updateFrames();
			}
			
		}
	}
	
}